﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing.Common;
using ZXing;
using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;

namespace CommandLib
{
    /// <summary>
    /// 跨平台一/二维码类
    /// </summary>
    public static class CodeImgHelper
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="message">生成二维码内容</param>
        /// <param name="gifFileName">图片路径</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static void CreateQrCode(string message, string gifFileName, int width = 600, int height = 600)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var w = new ZXing.QrCode.QRCodeWriter();
            BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, height);
            var zzb = new ZXing.ZKWeb.BarcodeWriter();
            {
                zzb.Options = new EncodingOptions() { Margin = 1 };
                Bitmap b2 = zzb.Write(b);
                b2.Save(gifFileName, ImageFormat.Gif);
                b2.Dispose();
            }

        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="message">二维码内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Bitmap CreateQrCode(string message, int width = 600, int height = 600)
        {
            Bitmap result = null;
            try
            {
                int heig = width;
                if (width > height)
                {
                    heig = height;
                    width = height;
                }
                var w = new ZXing.QrCode.QRCodeWriter();
                BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
                var zzb = new ZXing.ZKWeb.BarcodeWriter();
                zzb.Options = new EncodingOptions()
                {
                    Margin = 0,
                };
                result = zzb.Write(b);
            }
            catch (Exception)
            {

                throw;
            }
            return result;

        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="message">二维码内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Stream CreateQrCodeStream(string message, int width = 600, int height = 600)
        {
            Stream result = null;
            try
            {
                int heig = width;
                if (width > height)
                {
                    heig = height;
                    width = height;
                }
                var w = new ZXing.QrCode.QRCodeWriter();
                BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);
                var zzb = new ZXing.ZKWeb.BarcodeWriter();
                zzb.Options = new EncodingOptions()
                {
                    Margin = 0,
                };
                var bm = zzb.Write(b);
                result = new MemoryStream();
                bm.Save(result, ImageFormat.Png);
            }
            catch (Exception)
            {
                throw;
            }
            return result;

        }

        /// <summary>
        /// 生成BASE64二维码格式
        /// </summary>
        /// <param name="message">肉容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>

        public static string CreateQrCodeBase64(string message, int width = 600, int height = 600)
        {
            var result = string.Empty;
            var bmp = CreateQrCode(message, width, height);
            if (bmp != null)
            {
                result = Convert.ToBase64String(bmp.ToArray());
            }
            return result;

        }

        /// <summary>
        /// 读取二维码或者条形码从图片
        /// </summary>
        /// <param name="imgFile">图片路径</param>
        /// <returns></returns>
        public static string ReadFromImage(string imgFile)
        {
            string resultText = string.Empty;
            if (string.IsNullOrWhiteSpace(imgFile))
            {
                return "";
            }
            Image img = Image.FromFile(imgFile);
            Bitmap b = new Bitmap(img);
            try
            {
                //该类名称为BarcodeReader,可以读二维码和条形码
                var zzb = new ZXing.ZKWeb.BarcodeReader();
                zzb.Options = new DecodingOptions
                {
                    CharacterSet = "UTF-8"
                };
                Result res = zzb.Decode(b);
                resultText = res.Text;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                b.Dispose();
                img.Dispose();
            }
            return resultText;
        }

        /// <summary>
        /// 读取二维码或者条形码
        /// </summary>
        /// <param name="imgBase64">base64图片数据</param>
        /// <returns></returns>
        public static string ReadFromBase64(string imgBase64)
        {

            if (string.IsNullOrWhiteSpace(imgBase64))
            {
                return "";
            }
            var byteArray = Encoding.UTF8.GetBytes(imgBase64);
            Image img = byteArray.ToImage();
            Bitmap b = new Bitmap(img);
            //该类名称为BarcodeReader,可以读二维码和条形码
            var zzb = new ZXing.ZKWeb.BarcodeReader();
            zzb.Options = new DecodingOptions
            {
                CharacterSet = "UTF-8"
            };
            Result r = zzb.Decode(b);
            string resultText = r.Text;
            b.Dispose();
            img.Dispose();
            return resultText;

        }

        /// <summary>
        /// 生成条形码
        /// </summary>
        /// <param name="message">条形码内容</param>
        /// <param name="gifFileName">条形码路径</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        public static void CreateBarCode(string message, string gifFileName, int width, int height)
        {

            if (string.IsNullOrWhiteSpace(message))
            {
                return;
            }

            var w = new ZXing.OneD.CodaBarWriter();
            BitMatrix b = w.encode(message, BarcodeFormat.ITF, width, height);
            var zzb = new ZXing.ZKWeb.BarcodeWriter();
            zzb.Options = new EncodingOptions()
            {
                Margin = 3,
                PureBarcode = true
            };
            string dir = Path.GetDirectoryName(gifFileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (Bitmap b2 = zzb.Write(b))
            {
                b2.Save(gifFileName, ImageFormat.Png);
            }

        }

        //将Bitmap  写为byte[]的方法
        private static byte[] ToArray(this Bitmap bmp)
        {
            byte[] byteArray = null;
            using (var stream = new MemoryStream())
            {
                bmp.Save(stream, ImageFormat.Png);
                byteArray = stream.GetBuffer();
            }
            return byteArray;
        }
        //将Image  写为byte[]的方法
        private static Image ToImage(this byte[] bts)
        {
            Image bitmap = null;
            using (var stream = new MemoryStream())
            {
                stream.Write(bts, 0, bts.Length);
                bitmap = Image.FromStream(stream);
            }
            return bitmap;
        }
    }
}
